package org.poptest.util;

/**
 * Array Utilities. Some convenience methods for dealing with arrays.
 *
 * Created by IntelliJ IDEA.
 * License: MIT
 * User: alan
 * Date: 21/01/11
 * Time: 23:45
 */
public final class ArrayUtils {
    /**
     *
     *
     * @param array
     * @param object
     * @return true if array contains object
     */
    public static boolean contains(final Object[] array, final Object object) {
        return firstIndex(array, object) != -1;

    }

    /**
     * @param array
     * @param object
     * @return the index of the first element of the array that equals object, or -1 if no elements are equal
     */
    public static int firstIndex(final Object[] array, final Object object) {
        int index = -1;
        for (int i = 0; i < array.length; i++) {
            final Object element = array[i];
            if ((element == null && object == null) || (element != null && element.equals(object))) {
                index = i;
                break;
            }
        }
        return index;
    }
}
